/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Andreas Jakl (andreas.jakl@nokia.com)
**
** This file is part of an NFC tutorial for Qt Mobility.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
**     the names of its contributors may be used to endorse or promote
**     products derived from this software without specific prior written
**     permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef NDEFMANAGER_H
#define NDEFMANAGER_H

#include <QObject>
#include <QDebug>
#include <QUrl>
#include <QDeclarativeView>
#include <QSystemInfo>

#include <qnearfieldmanager.h>
#include <qndeffilter.h>
#include <qnearfieldtarget.h>
#include <qndefmessage.h>
#include <qndefrecord.h>
#include <qndefnfcurirecord.h>
#include <qndefnfctextrecord.h>
#include "ndefnfcmimeimagerecord.h"
#include "ndefnfcsprecord.h"
#include "ndefnfcmimevcardrecord.h"

#include <QContact>
#include <QContactDisplayLabel>
#include <QContactEmailAddress>
#include <QContactPhoneNumber>

QTM_USE_NAMESPACE

#ifdef Q_OS_SYMBIAN
#include "nfcsettings.h"
#endif

class NdefManager : public QObject
{
    Q_OBJECT
public:
    explicit NdefManager(QObject *parent = 0);

    void setDeclarativeView(QDeclarativeView& view);

signals:
    void nfcStatusUpdate(const QString& nfcStatusText);
    void nfcStatusError(const QString& nfcStatusErrorText);
    void nfcReadTagUri(const QUrl& nfcTagUri);
    void nfcReadTagText(const QString& nfcTagText);
    void nfcReadTagSp(const QUrl& nfcTagUri, const QString& nfcTagTitle);
    void nfcReadTagVcard(const QString& nfcTagName, const QString& nfcTagEmail, const QUrl& nfcTagPhone);
    void nfcReadTagAutostart(const QByteArray& nfcTagPayload);
    void nfcReadTagUnknown(const QByteArray& nfcTagType, const QByteArray& nfcTagPayload);
    void nfcReadTagError(const QString& nfcTagError);

public slots:
    void checkNfcStatus();
    void initAndStartNfc();
    void nfcWriteTag(const QString& nfcTagText);


private slots:
    void targetMessageDetected(const QNdefMessage &message, QNearFieldTarget *target);
    void targetDetected(QNearFieldTarget *target);
    void ndefMessageRead(const QNdefMessage &message);
    void requestCompleted(const QNearFieldTarget::RequestId & id);
    void ndefMessageWritten();
    void targetError(QNearFieldTarget::Error error, const QNearFieldTarget::RequestId &id);
    void targetLost(QNearFieldTarget *target);

private:
    void writeCachedNdefMessage();
    QString convertTargetErrorToString(QNearFieldTarget::Error error);

#ifdef Q_OS_SYMBIAN
private slots:
    // Check for NFC Support
    void handleNfcError(NfcSettings::NfcError nfcError, int error);
    void handleNfcModeChange(NfcSettings::NfcMode nfcMode);
private:
    NfcSettings* nfcSettings;
#endif

private:
    QNearFieldManager *nfcManager;
    QNearFieldTarget *cachedTarget;
    bool pendingWriteNdef;
    QNdefMessage cachedNdefMessage;
    QNearFieldTarget::RequestId writeRequestId;
    QDeclarativeView* declarativeView;
    /*! Running on Harmattan PR 1.0? Then, need to switch between reading
      and writng NDEF messages from/to tags, as this FW can't have both
      modes activated at the same time. This has been improved in PR 1.1+. */
    bool m_harmattanPr10;
};

#endif // NDEFMANAGER_H
